# -*- coding: utf-8 -*-  

from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from contextlib import closing
from soufang import app, db
from models import Urls, Fangs
import urllib
import urllib2
import re
from sqlalchemy import and_, desc

@app.route('/', methods = ['GET', 'POST'])
@app.route('/index', methods = ['GET', 'POST'])
@app.route('/index/<int:page>', methods = ['GET', 'POST'])
def show_fangs(page = 1):
	fangs = Fangs.query.order_by(desc(Fangs.id)).paginate(page, 20, False)
	return render_template('show_entries.html', fangs=fangs)

'''
@app.route('/add', methods=['POST'])
def add_entry():
	if not session.get('logged_in'):
		abort(401)
	g.db.execute('insert into entries (title, text) values (?, ?)', [request.form['title'], request.form['text']])
	g.db.commit()
	flash('New entry was successfully posted')
	return redirect(url_for('show_entries'))
'''
@app.route('/login', methods=['GET', 'POST'])
def login():
	error = None
	if request.method == 'POST':
		if request.form['username'] != app.config['USERNAME']:
			error = 'Invalid username'
		elif request.form['password'] != app.config['PASSWORD']:
			error = 'Invalid password'
		else:
			session['logged_in'] = True
			flash('You were logged in')
			return redirect(url_for('show_fangs'))
	return render_template('show_entries.html', error=error)

@app.route('/logout')
def logout():
	session.pop('logged_in', None)
	flash('You were logged out')
	return redirect(url_for('show_entries'))

@app.route('/hidefangs/<fangsid>/<group>')
def hidefangs(fangsid, group):
	fang = Fangs.query.filter_by(id=fangsid).first()
	
	if group == 'yes':
		fangs = Fangs.query.filter_by(zhname=fang.zhname).all()
		for fang in fangs:
			fang.hidden = "yes"

	else:
		fang = Fangs.query.filter_by(id=fangsid).first()
		fang.hidden = "yes"
		
	db.session.commit()
	return redirect(url_for('show_fangs'))
			
@app.route('/refresh', methods=['GET', 'POST'])
def refresh(page = 1):
	allurllist = []
	newurllist = []
	numfangs = 0
	postDict = {
		'pricemin':'40',
		'pricemax':'52',
		'areamin':'76',
		'areamax':'110',
		'purpose':'%D7%A1%D5%AC',
		'city':'%CD%FE%BA%A3',
		'district':'%BE%AD%C7%F8',
		'orderby':'0',
		'keywordtype':'qz',
		'gettype':'android',
		'correct':'true',
		'c':'esf',
		'a':'ajaxGetList',
		'city':'weihai',
		'r':'0.04594909260049462',
		'page':'3'
	};
	postData = urllib.urlencode(postDict).strip()
	defHeaderDict = {
		'User-Agent'    : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)',
		'Cache-Control' : 'no-cache',
		'Accept'        : '*/*',
		'Connection'    : 'Keep-Alive',
	};
	headerDict = {
		'Referer': 'http://m.fang.com/esf/weihai/m40%2C52a76,110/'
	};
	while 1:
		url = "http://m.fang.com/esf/?pricemin=40&pricemax=52&areamin=76&areamax=110&purpose=%D7%A1%D5%AC&district=%BE%AD%C7%F8&city=%CD%FE%BA%A3&orderby=0&keywordtype=qz&gettype=android&correct=true&c=esf&a=ajaxGetList&city=weihai&r=0.04594909260049462&page=" + str(page)
		url = str(url)
		req = urllib2.Request(url, postData);
		req.add_header('Content-Type', "application/x-www-form-urlencoded");
		for eachDefHd in defHeaderDict.keys() :
			req.add_header(eachDefHd, defHeaderDict[eachDefHd]);
		for key in headerDict.keys() :
			req.add_header(key, headerDict[key]);
		respUnicodeHtml = urllib2.urlopen(req).read().decode('utf-8')
		urlList = re.findall('<a.*?id="".*?href="(.*?)">.*?</a>',respUnicodeHtml,re.S)
		
		if len(urlList) > 0 and len(allurllist) < 500 :
			page += 1
			allurllist.extend(urlList)
		else :
			break
			
	for singleurl in allurllist:
		haveurl = Urls.query.filter_by(addr=singleurl).all()
		if len(haveurl) == 0:
			newurllist.append(singleurl)
			surl = Urls()
			surl.addr=singleurl
			db.session.add(surl)
			db.session.commit()

	#urls = Urls.query.all()
	for singleurl in newurllist :
		req = urllib2.Request(str(singleurl))
		respUnicodeHtml = urllib2.urlopen(req).read().decode('gbk','ignore')
		myFangs = re.findall('<span.*?id="xiaoqu">(.*?)</span>.*?<p.*?id="agent_name">(.*?)</p>.*?<div.*?id="storageimg">(.*?)</div>.*?<div.*?id="phone">(.*?)</div>.*?<div.*?id="title">(.*?)</div>.*?<div.*?id="price">(.*?)</div>.*?<div.*?id="room">(.*?)</div>.*?<div.*?id="area">(.*?)</div>.*?<div.*?id="zhname">(.*?)</div>.*?<div.*?id="purpose">(.*?)</div>.*?<div.*?id="district">(.*?)</div>.*?<div.*?id="comarea">(.*?)</div>.*?</div>',respUnicodeHtml,re.S)
		if len(myFangs) > 0 :
			havefang = Fangs.query.filter(and_(Fangs.phone == myFangs[0][3], Fangs.title == myFangs[0][4])).all()
			if len(havefang) == 0 :
				sfang = Fangs()
				sfang.xiaoqu = myFangs[0][0]
				sfang.agent_name = myFangs[0][1]
				sfang.storageimg = myFangs[0][2]
				sfang.phone = myFangs[0][3]
				sfang.title = myFangs[0][4]
				sfang.price = myFangs[0][5]
				sfang.room = myFangs[0][6]
				sfang.area = myFangs[0][7]
				sfang.zhname = myFangs[0][8]
				sfang.purpose = myFangs[0][9]
				sfang.district = myFangs[0][10]
				sfang.comarea = myFangs[0][11]
				sfang.url = singleurl
				
				numfangs += 1
				
				db.session.add(sfang)
				db.session.commit()
	message = str(numfangs) + ' new fangs update'
	flash(message)
	return redirect(url_for('show_fangs'))